
#ifndef  CACHE_H
#define  CACHE_H

#include "common.h"
class FreeList;
class Page;

class Cache {
public:
    Cache(const int64_t cacheSize, const int32_t pageSize);
    ~Cache();
    int init();
    void deinit();

    bool isAllocated(const PageNumberType pageNo) const { return pageMap.find(pageNo) != pageMap.end(); }
    Page * allocate(PageNumberType pageNo);
    Page * acquire(PageNumberType pageNo);
    int release(Page * page);

    const int32_t freePageCount() const ;
    const int32_t usedPageCount() const { return pageMap.size(); }
    const int32_t freeBytes() const { return freePageCount() * pageSize; }
    const int32_t usedBytes() const { return usedPageCount() * pageSize; }

private:
    const int64_t cacheSize;
    const int32_t pageSize;

    char * pageAddr;  //start address of  freeList
    FreeList  * freeList;
    std::unordered_map<PageNumberType, Page *> pageMap;
};


#endif   /* CACHE_H */
